/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

/* eslint-disable react/jsx-no-bind */
/* eslint-disable react/sort-comp */
import React, { Component } from 'react';
import { connect } from 'dva';
import { Alert } from 'antd';
import { debounce } from 'lodash';
import AreaList from '@/components/AreaList';
import InfoCheck from '@/components/InfoCheck';
import Search from './Search';
import Pagination from './Pagination';

@connect(({ loading, area, user, setting }) => ({
  area,
  user,
  loading: loading.effects['area/subList'],
  ...setting,
}))
export default class Audit extends Component {
  state = {
    current: 1,
    count: 1,
    pageSize: 10,
  };

  handleInfoReady(data) {
    const {
      options: { key },
    } = this.props;
    if (!data.name && key !== 'post') return;
    this.getAreaList(1);
  }

  getAreaList(page) {
    const {
      dispatch,
      area,
      user: {
        currentUser: { user_info },
      },
      options: { key },
    } = this.props;
    const {
      audit: { keyword, branch_id, status },
    } = area;
    const { area_ids, inn_area_ids = [] } = user_info;

    if (key == 'post') {
      dispatch({
        // 中邮获取待审核驿站
        type: 'area/zyGetSubPostStationsToBeChecked',
        payload: {
          branch_id: branch_id || user_info.branchId,
          keyword: keyword || '',
          status,
          page,
          pageSize: 20,
        },
      });
    } else {
      // 片区初始值
      const pianquReqData =
        inn_area_ids.length > 0 ? { area_id: area_ids == '*' ? 0 : inn_area_ids.join(',') } : {};
      dispatch({
        type: 'area/subList',
        payload: {
          is_district: '2',
          key: '3',
          keyword: keyword || '',
          pageNum: page,
          ...pianquReqData,
        },
      });
    }
  }

  getAreaRefusList(page) {
    const {
      dispatch,
      area,
      user: {
        currentUser: { user_info },
      },
      options: { key },
    } = this.props;
    const { audit } = area;

    if (key === 'post') {
      dispatch({
        // 中邮获取待审核驿站
        type: 'area/zyGetSubPostStationsToBeRefus',
        payload: {
          branch_id: user_info.branchId,
          keywords: audit.keyword ? audit.keyword : '',
          page,
          pageSize: 20,
        },
      });
    } else {
      dispatch({
        type: 'area/subList',
        payload: {
          is_district: '3',
          key: '3',
          pageNum: page,
        },
      });
    }
  }

  // 分页
  onChange = debounce(
    page => {
      this.getAreaList(page);
      this.setState({
        current: page,
      });
    },
    500,
    {
      leading: true,
      trailing: false,
    },
  );

  render() {
    const {
      loading,
      area,
      options: { key },
    } = this.props;
    const { current, count, pageSize } = this.state;
    const { audit } = area;
    const { paginations } = audit;
    const currentPage = paginations ? paginations.page : current;
    const totalNum = paginations ? paginations.count : count;
    const pageSizeNum = paginations ? paginations.pageSize : pageSize;
    return (
      <div>
        {key !== 'post' && (
          <Alert
            style={{ marginBottom: 24 }}
            showIcon
            message={
              <span>
                审核通过前，该驿站将无法使用出入库功能；其他功能可正常使用。48小时内未审核的，自动审核通过。
                <br />
                审核通过后，该驿站将出现在“区域内独立驿站”。审核拒绝后，该驿站将出现在‘已拒绝驿站’，可重新通过审核。
                <br />
                如需将该驿站添加为下属驿站，请和驿站联系，并使用“添加新的下属驿站”功能。
              </span>
            }
            type="warning"
          />
        )}
        <InfoCheck onReady={this.handleInfoReady.bind(this)} />
        <Search searchType="3" current={currentPage} />
        <AreaList
          loading={loading}
          data={audit}
          post="3"
          getAreaRefusList={() => this.getAreaRefusList(1)}
          getList={() => this.getAreaList(current)}
        />
        <Pagination
          current={currentPage * 1}
          count={totalNum}
          onChange={this.onChange}
          pageSize={pageSizeNum}
        />
      </div>
    );
  }
}
